#include <iostream>  
using namespace std;

int find(int *f,int *v,int x){
	if(f[x] == x)
		return x;
	int t=f[x];
	f[x]=find(f,v,t);
	v[x]=(v[x]+v[t])%3;
	return f[x];
}


int main(){
	int n,m,ans=0;
	cin>>n>>m;
	int f[n],v[n];
	for(int i=0;i<n;i++) f[i]=i,v[i]=0;
	for(int i=0;i<m;i++){
		int x,y,k;
   		cin>>k>>x>>y;
   		x--;
   		y--;
		if((x>n||y>n)||(k==2&&x==y)){
			ans++;
			continue;
		}
		if(k==1){
			int fx=find(f,v,x),fy=find(f,v,y);
   			if(fx==fy&&v[x]!=v[y]){
				ans++;
				continue;
	   		}
   			else if(fx!=fy){
				f[fx]=fy; v[fx]=(3-v[x]+v[y])%3; 
			}
		}
		if(k == 2){
			int fx=find(f,v,x),fy=find(f,v,y);
			if (fx==fy) {
				int vla=(v[x]-v[y]+3)%3; 
				if (vla!=1){
					ans++;
					continue;
				}
			}
			else{
				int fx=find(f,v,x),fy=find(f,v,y);
				f[fx]=fy;
				v[fx]=(-v[x]+v[y]+1+3)%3; 
			}
		}
	}
	cout<<ans;
	return 0;
}
